const express = require('express')//express框架
const { Op } = require('sequelize')//nodejs的sequelize模块
const order = require('../crud/table/myorder.js')//引入订单信息表
const customer = require('../crud/table/customer.js')//引入顾客信息表
const room = require('../crud/table/room.js')//引入房间信息表

order.hasOne(customer,{ sourceKey:'customerIdCard', foreignKey:'customerIdCard' })
order.hasOne(room,{ sourceKey:'roomNumber', foreignKey:'roomNumber' })

const orderRouter = express.Router()

orderRouter.post('/add',(req,res) =>{//创建订单
    console.log(req.body)
    const { orderNumber, orderStatus,customerIdCard,roomNumber,
            checkInTime,checkOutTime,totalMoney,remarks
        } = req.body;
    order.findOne({
        where:{
            orderNumber
        }
    })
    .then(data =>{
        if(data){
            res.status(200).json({
                success:false,
                msg:'该订单已经存在！'
            })
            return new Promise(() =>{})
        }else{
            return customer.findOne({
                where:{
                    customerIdCard
                }
            })
        }
    })
    .then(data =>{
        if(data){
            return room.update({
                    roomStatus:'已入住'
            },{
                where:{
                        roomNumber
                }
            })
        }else{
            res.status(200).json({
                success:false,
                msg:'该用户还未注册！'
            });
            return new Promise(() =>{});
        }
       
    })
    .then(data =>{
        return order.create({
            orderNumber, orderStatus,customerIdCard,roomNumber,
            checkInTime,checkOutTime,totalMoney,remarks
        })
    })
    .then(data =>{
        res.status(200).json({
            success:'true',
            msg:'订单创建成功！'
        })
    })
    .catch(err =>{
        res.status(200).json({
            success:false,
            msg:'内部服务器出错！'
        })
    })
})

orderRouter.delete('/del',(req,res) =>{//删除订单
    const { orderNumber } = req.body;
    order.findOne({
        where:{
            orderNumber
        }
    })
    .then(data =>{
        if(data){
            return order.destroy({
                where:{
                    orderNumber
                }
            })
        }else{
            res.status(200).json({
                success:false,
                msg:'该订单不存在！'
            })
            return new Promise(() =>{})
        }
    })
    .then(data =>{
        res.status(200).json({
            success:true,
            msg:'删除成功！'
        })
    })
    .catch(err =>{
        res.status(200).json({
            success:false,
            msg:'服务器内部错误！'
        })
    })
})

orderRouter.put('/update',(req,res) =>{//修改订单状态
    const { orderStatus, orderNumber,totalMoney,roomNumber } = req.body;
    order.findOne({
        where:{
            orderNumber
        }
    })
    .then(data =>{
        if(data){
            return room.update({
                roomStatus:'未入住'
            },{
                where:{
                    roomNumber
                }
            })
        }else{
            res.status(200).json({
                success:false,
                msg:'该订单不存在！'                
            })
            return new Promise(() =>{})
        }
    })
    .then(data =>{
        return order.update({
            orderStatus,totalMoney
        },{
            where:{
                orderNumber
            }
        })
       
    })
    .then(data =>{
        return order.findOne({
            where:{
                orderNumber
            }
        })
    })
    .then(data =>{
        res.status(200).json({
            success:true,
            msg:'订单修改成功！',
            order:data.get()
        })
    })
    .catch(err =>{
        res.status(200).json({
            success:false,
            msg:'服务器内部错误！'
        })
    })

})

orderRouter.get('/getAllOrder',(req,res) =>{//查询所有订单
    const { pageSize, offset } = req.query;
    order.findAndCountAll({
        limit:pageSize,
        offset:offset,
        include:[
            {
                model:customer,
                foreignKey:'customerIdCard',
                attributes:['customerName','customerSex','customerVipLevel','customerPhoneNumber','totalAmount']
            },
            {
                model:room,
                foreignKey:'roomNumber',
                attributes:['type','remarks']
            }
        ]
    })
    .then(data =>{
        res.status(200).json({
            success:true,
            msg:'所有订单查询成功！',
            orderList:data.rows.map(item =>{
                return item.get()
            }),
            count:data.count
        })
    })
    .catch(err =>{
        res.status(200).json({
            success:false,
            msg:'服务器内部错误!'
        })
    })
})

orderRouter.get('/getAllNotPayOrder',(req,res) =>{//查询所有未支付的订单
    const { pageSize, offset } = req.query;
    order.findAndCountAll({
        limit:pageSize,
        offset:offset,
        where:{
            orderStatus:{
                [Op.eq]:'未支付'
            }
        },
        include:[
            {
                model:customer,
                foreignKey:'customerIdCard',
                attributes:['customerName','customerSex','customerVipLevel','customerPhoneNumber','totalAmount']
            },
            {
                model:room,
                foreignKey:'roomNumber',
                attributes:['type']
            }
        ]
    })
    .then(data  =>{
        res.status(200).json({
            success:true,
            msg:'未支付订单查询成功！',
            orderList:data.rows.map(item =>{
                return item.get()
            }),
            count:data.count,
            pageSize:pageSize,
            offset:offset
        })
    })
    .catch(err =>{
        res.status(200).json({
            success:false,
            msg:'服务器内部错误！'
        })
    })
})

orderRouter.get('/getAllPayOrder',(req,res) =>{//查询所有已支付的订单
    const { pageSize, offset } = req.query;
    order.findAndCountAll({
        limit:pageSize,
        offset:offset,
        where:{
            orderStatus:{
                [Op.eq]:'已支付'
            }
        },
        include:[
            {
                model:customer,
                foreignKey:'customerIdCard',
                attributes:['customerName','customerSex','customerVipLevel','customerPhoneNumber','totalAmount']
            },
            {
                model:room,
                foreignKey:'roomNumber',
                attributes:['type']
            }
        ]
    })
    .then(data  =>{
        res.status(200).json({
            success:true,
            msg:'未支付订单查询成功！',
            orderList:data.rows.map(item =>{
                return item.get()
            }),
            count:data.count,
            pageSize:pageSize,
            offset:offset
        })
    })
    .catch(err =>{
        res.status(200).json({
            success:false,
            msg:'服务器内部错误！'
        })
    })
})

orderRouter.get('/getStatusOrder',(req,res) =>{//查询所有该状态的订单
    const { pageSize, offset, orderStatus } = req.query;
    order.findAndCountAll({
        limit:pageSize,
        offset:offset,
        where:{
            orderStatus:{
                [Op.eq]:orderStatus
            }
        },
        include:[
            {
                model:customer,
                foreignKey:'customerIdCard',
                attributes:['customerName','customerSex','customerVipLevel','customerPhoneNumber','totalAmount']
            },
            {
                model:room,
                foreignKey:'roomNumber',
                attributes:['type']
            }
        ]
    })
    .then(data  =>{
        res.status(200).json({
            success:true,
            msg:'状态订单查询成功！',
            orderList:data.rows.map(item =>{
                return item.get()
            }),
            count:data.count,
            pageSize:pageSize,
            offset:offset
        })
    })
    .catch(err =>{
        res.status(200).json({
            success:false,
            msg:'服务器内部错误！'
        })
    })
})
module.exports = orderRouter;